<?php
ob_start();
session_cache_expire(999999999);
if (!isset($_SESSION)) {
    session_start();
}
if($_SESSION['role']!=2){
	echo $_SESSION['role'];
	header("Location: ../index.php");
}
if (isset($_REQUEST['action'])) {
    include 'operations/connection.php';
    $type = $_REQUEST['type'];
    if ($type == '11') {
        $idapprovedoc = $_REQUEST['idapprovedoc'];
        $listapprove = $_SESSION['listapprove'];

        $a = (string) (microtime(true) * 1000);
        $b = split('\.', $a);
        $idgroupapprove = $b[0];
        if (count($listapprove) > 0) {
            // update approvedoc
            $sql = "UPDATE approvedoc SET IDGroupApprove='$idgroupapprove', IDStatus='2' WHERE (IDApproveDoc='$idapprovedoc')";
            mysql_query($sql);
            foreach ($listapprove as $approve => $abc) {

                $sql = "INSERT INTO `listapprove` (`IDGroupApprove`, `IDEmployee`, `IDtypeApprove`, `Order`, `IDStatus`) VALUES ('$idgroupapprove', '$approve', '$type', '1', '2')";
                mysql_query($sql);
            }
            header("Location: mydocument-list-mydocument-meeting-sending-content");
        }
    } else {



        $idapprovedoc = $_REQUEST['idapprovedoc'];
        $listapprove = $_SESSION['listapprove'];
        $listapprovename = $_SESSION['listapprovename'];


        $a = (string) (microtime(true) * 1000);
        $b = split('\.', $a);
        $idgroupapprove = $b[0];
        if (count($listapprove) > 0) {
            // update approvedoc
            $sql = "UPDATE approvedoc SET IDGroupApprove='$idgroupapprove', IDStatus='2' WHERE (IDApproveDoc='$idapprovedoc')";

            mysql_query($sql);

            // insert  list group approve
            $i = 1;
            $employee_id = "";
            foreach ($listapprove as $approve => $abc) {
                if ($i == 1) {
                    $employee_id = $approve;
                    $sql = "INSERT INTO `listapprove` (`IDGroupApprove`, `IDEmployee`, `IDtypeApprove`, `Order`, `IDStatus`) VALUES ('$idgroupapprove', '$approve', '$type', '$i', '2')";
                    mysql_query($sql);
                } else {
                    $sql = "INSERT INTO `listapprove` (`IDGroupApprove`, `IDEmployee`, `IDtypeApprove`, `Order`, `IDStatus`) VALUES ('$idgroupapprove', '$approve', '$type', '$i', '4')";
                    mysql_query($sql);
                }

                foreach ($abc as $hienthi) {
                    $sql = "INSERT INTO list_reference (id_employee,id_manager,id_group,id_status) VALUES ('$hienthi[0]', '$approve', '$idgroupapprove', '4')";
                    mysql_query($sql);
                }
                $i = $i + 1;
            }
            if ($_REQUEST['group'] != '') {
                $sql = "INSERT INTO groupapprove (id,employee_id,name) VALUES ('$idgroupapprove', '" . $_SESSION['IDEmployee'] . "', '" . $_REQUEST['group'] . "')";
                mysql_query($sql);
            }


            // send mail
            $addressbrowser = $_SERVER['SERVER_NAME'];
            $temp = explode(".", $addressbrowser);
            $lang = $_SESSION['lang'];
            if ($lang == '') {
                $lang = 'EN';
                $_SESSION['lang'] = "EN";
            }


            mysql_select_db("elihan", $connection);

            $sql = "INSERT INTO sendingmail(idgroupapprove,id_approvedoc,id_employee,company,lang,co,id_employee_last) VALUES ('$idgroupapprove', '$idapprovedoc','$employee_id', '$temp[0]', '$lang','0','$employee_id')";
            mysql_query($sql);


            mysql_close($connection);
            //set POST variables
            /*
              $addressbrowser = $_SERVER['SERVER_NAME'];
              $temp = explode(".", $addressbrowser);
              $lang=$_SESSION['lang'];
              if($lang==''){
              $lang='EN';
              $_SESSION['lang']="EN";
              }

              $url = 'http://1.234.53.52/mail/index.php';
              $fields = array(

              'idgroupapprove' => urlencode($idgroupapprove),
              'id_approvedoc' => urlencode($idapprovedoc),
              'id_employee' => urlencode($employee_id),
              'company' => urlencode($temp[0]),
              'lang' => urlencode($lang),
              'co' => 0,
              'id_employee_last' => urlencode($employee_id)
              );

              //url-ify the data for the POST
              foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
              rtrim($fields_string, '&');

              //open connection
              $ch = curl_init();

              //set the url, number of POST vars, POST data
              curl_setopt($ch,CURLOPT_URL, $url);
              curl_setopt($ch,CURLOPT_POST, count($fields));
              curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

              //execute post
              $result = curl_exec($ch);

              //close connection
              curl_close($ch);
             */

            header("Location: mydocument-list-mydocument-sending-content");
        }
    }
}


include 'operations/connection.php';
$idapprovedoc = $_REQUEST['idapprovedoc'];
$type = $_REQUEST['type'];

$lang = $_SESSION['lang'];
if ($lang == '')
    $lang = 'EN';
include("operations/get_lang.php");
$value = getlang();
include($value);

$content = "content/approve-content.php";
if ($type == '11') {
    $content = "content/approvemore-content.php";
}


if (!isset($_SESSION['user'])) {
    header('Location: login.php?timeout=on');
} else {
    $username = $_SESSION['fullname'];
    $id_employee = $_SESSION['IDEmployee'];
}
echo $_SESSION['back'];
$title = $_SESSION['browser'];


if ($_SESSION['role'] == 1) {

    require("MainTemplateAdmin.php");
} else {
    require("MainTemplate.php");
}
ob_flush();
?>